package com.winit.usTask.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.commons.util.HttpClientUtil;
import com.winit.commons.util.SeqGenerate;
import com.winit.usTask.mapper.UsTaskMapper;
import com.winit.usTask.model.UsTask;
import com.winit.usTask.service.UsTaskService;
import com.winit.uschedule.manager.UsTaskDefineManager;
import com.winit.uschedule.manager.UsTaskManager;
import com.winit.uschedule.model.UsTaskDefine;
import com.winit.uschedule.model.UscheduleTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wanglin on 2018/6/4.
 */

@Service
public class UsTaskServiceImpl implements UsTaskService{
    private static Logger log = LoggerFactory.getLogger(UsTaskServiceImpl.class);

    @Autowired
    UsTaskMapper usTaskMapper;

    @Autowired
    UsTaskDefineManager usTaskDefineManager;
    @Autowired
    UsTaskManager usTaskManager;

    @Override
    public void initUschedule() {
        log.info("开始刷新uschedule数据。。。。");
        List<UsTaskDefine> usTaskDefines = usTaskDefineManager.getAll();
        for(UsTaskDefine usTaskDefine:usTaskDefines){
            Map<String, Object> selectMap = new HashMap<>();
            selectMap.put("sys",usTaskDefine.getGroupName());
            selectMap.put("name",usTaskDefine.getTaskName());
            UsTask oldUsTask = usTaskMapper.findBySysAndName(selectMap);
            if(null != oldUsTask){
                continue;
            }
            UsTask usTask = new UsTask();
            usTask.setId(SeqGenerate.createId("USTA"));
            usTask.setSys(usTaskDefine.getGroupName());
            usTask.setName(usTaskDefine.getTaskName());
            usTask.setInterfaceType(usTaskDefine.getInterfaceType());
            usTask.setTaskDealClass(usTaskDefine.getTaskDealClass());
            if("SCHEDULE_TASK_MUTIL".equals(usTaskDefine.getInterfaceType())){
                UscheduleTask uscheduleTask = usTaskManager.findById(usTaskDefine.getGroupName() + "$" + usTaskDefine.getTaskName());
                if(uscheduleTask!=null){
                    usTask.setParam(uscheduleTask.getTaskParameter());
                    usTask.setItem(uscheduleTask.getTaskItem());
                }
            }
            usTaskMapper.insertSelective(usTask);
        }
        log.info("结束刷新uschedule数据。。。。");
    }

    @Override
    public ExecuteResult getGroups(String type){
        ExecuteResult er = new ExecuteResult();

        er.setData(usTaskMapper.getGroups(type));
        return er;
    }

    @Override
    public ExecuteResult getTasks(String type, String sys) {
        ExecuteResult er=new ExecuteResult();
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("type",type);
        selectMap.put("sys",sys);
        er.setData(usTaskMapper.getUsTasks(selectMap));
        return er;
    }

    @Override
    public UsTask getTaskNameAndSys(String name, String sys) {
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("name",name);
        selectMap.put("sys",sys);
        return usTaskMapper.findBySysAndName(selectMap);
    }

    @Override
    public ExecuteResult runUsTask(String ip, String post, String sys, String taskName, String uId) {
        ExecuteResult er=new ExecuteResult();
        //           sys=group
        String url="taskGroup="+sys+"&taskName="+taskName;
        UsTask task = this.getTaskNameAndSys(taskName, sys);
        url+="&taskClass="+task.getTaskDealClass()+"&tId="+sys+"$"+taskName+"&taskInterfaceType="+task.getInterfaceType()+"&uId="+uId;
        if("SCHEDULE_TASK_MUTIL".equals(task.getInterfaceType())){
            url+="&taskParameter="+task.getParam()+"&tItem="+task.getItem();
        }
        try{
            String json = HttpClientUtil.doGet("http://"+ip+":"+post+"/testUsTask?"+url);
        }catch (Exception e){
            er.setDesc("调用uschedule任务失败，请联系Beetle管理员");
            er.setCode("1");
            return er;
        }
        return er;
    }

}
